修改後>按聯絡我們會報錯,因為共用通知
顯示
程式碼
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\User;
class WebController extends Controller
{
public function index()
{
$products =Product::all();
$user=User::find(3);
$notifications = $user->notifications ?? [];
return view('web.index',['products' =>$products, 'notifications' =>$notifications]);
}
public function contactUs()
{
return view('web.contact_us');
}
修改程式碼
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\User;
use Illuminate\Notifications\DatabaseNotification;
class WebController extends Controller
{
public $notifications = [];
public function __construct()
{
$user=User::find(3);
$this->notifications = $user->notifications ?? [];
}
public function index()
{
$products =Product::all();
return view('web.index',['products' =>$products, 'notifications' =>$this->notifications]);
}
public function contactUs()
{
return view('web.contact_us', ['notifications' =>$this->notifications]);
}
public function readNotification(Request $request)
{
$id = $request->all()['id'];
DatabaseNotification::find($id)->markAsRead();
return response(['result'=>true]);
}
}
資料表14/15/16
用POSTMAN的POST去通知
+
資料表看到
改成user都是3
就可以看到4個通知
跟
修正程式碼
確認數量的按鈕修好
修改前端程式碼
<ul>
@foreach ($notifications as $notification)
<li class="read_notification" data-id="{{ $notification->id }}">{{ $notification->data['msg'] }}
<span class="read">
@if ($notification->read_at)
(已讀)
@endif
</span>
</li>
@endforeach
</ul>
</div>
</div>
設定路由
畫面上面按加入_ 看到已讀
資料表加上read_at欄位就是紀錄是否 被讀過欄位
被讀過欄位
大家明天見~